Skip to content

Method: getMapForClass(URI, Class)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.cache;
16:
17: import java.net.URI;
18: import java.util.Collections;
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: /**
23: * TODO Think about locking on context level, so that the whole cache doesn't have to be locked when being accessed
24: * @author ledvima1
25: */
26: class EntityCache {
27:
28: private static final String DEFAULT_CONTEXT_BASE = "http://defaultContext";
29:
30: final Map<URI, Map<Class<?>, Map<Object, Object>>> repoCache;
31: final URI defaultContext;
32:
33: EntityCache() {
34: repoCache = new HashMap<>();
35: this.defaultContext = URI.create(DEFAULT_CONTEXT_BASE + System.currentTimeMillis());
36: }
37:
38: void put(Object primaryKey, Object entity, URI context) {
39: assert primaryKey != null;
40: assert entity != null;
41:
42: final Class<?> cls = entity.getClass();
43: final URI ctx = context != null ? context : defaultContext;
44:
45: Map<Class<?>, Map<Object, Object>> ctxMap;
46: if (!repoCache.containsKey(ctx)) {
47: ctxMap = new HashMap<>();
48: repoCache.put(ctx, ctxMap);
49: } else {
50: ctxMap = repoCache.get(ctx);
51: }
52: Map<Object, Object> clsMap;
53: if (!ctxMap.containsKey(cls)) {
54: clsMap = new HashMap<>();
55: ctxMap.put(cls, clsMap);
56: } else {
57: clsMap = ctxMap.get(cls);
58: }
59: clsMap.put(primaryKey, entity);
60: }
61:
62: <T> T get(Class<T> cls, Object primaryKey, URI context) {
63: assert cls != null;
64: assert primaryKey != null;
65:
66: final URI ctx = context != null ? context : defaultContext;
67: final Map<Object, Object> m = getMapForClass(ctx, cls);
68: if (m.containsKey(primaryKey)) {
69: return cls.cast(m.get(primaryKey));
70: }
71: return null;
72: }
73:
74: boolean contains(Class<?> cls, Object primaryKey) {
75: assert cls != null;
76: assert primaryKey != null;
77: final Map<Object, Object> m = getMapForClass(defaultContext, cls);
78: return m.containsKey(primaryKey);
79: }
80:
81: boolean contains(Class<?> cls, Object primaryKey, URI context) {
82: assert cls != null;
83: assert primaryKey != null;
84: if (context == null) {
85: return contains(cls, primaryKey);
86: }
87:
88: final Map<Object, Object> m = getMapForClass(context, cls);
89: return m.containsKey(primaryKey);
90: }
91:
92: void evict(Class<?> cls, Object primaryKey, URI context) {
93: assert cls != null;
94: assert primaryKey != null;
95:
96: final URI ctx = context != null ? context : defaultContext;
97: final Map<Object, Object> m = getMapForClass(ctx, cls);
98: if (m.containsKey(primaryKey)) {
99: m.remove(primaryKey);
100: }
101: }
102:
103: void evict(URI context) {
104: if (context == null) {
105: context = defaultContext;
106: }
107: if (!repoCache.containsKey(context)) {
108: return;
109: }
110: repoCache.get(context).clear();
111: }
112:
113: void evict(Class<?> cls) {
114: for (Map.Entry<URI, Map<Class<?>, Map<Object, Object>>> e : repoCache.entrySet()) {
115: final Map<Class<?>, Map<Object, Object>> m = e.getValue();
116: m.remove(cls);
117: }
118: }
119:
120: private Map<Object, Object> getMapForClass(URI context, Class<?> cls) {
121:• assert context != null;
122:• assert cls != null;
123:
124:• if (!repoCache.containsKey(context)) {
125: return Collections.emptyMap();
126: }
127: final Map<Class<?>, Map<Object, Object>> ctxMap = repoCache.get(context);
128:• return (ctxMap.containsKey(cls) ? ctxMap.get(cls) : Collections.emptyMap());
129: }
130: }